package com.bit.cfdi.web.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bit.cfdi.web.dao.SerieDeletedDAO;
import com.bit.cfdi.web.domain.SerieDeleted;
import com.bit.cfdi.web.domain.Sucursal;

@Repository
public class SerieDeletedDAOImpl extends HibernateDaoSupport 
	implements SerieDeletedDAO {

	@Autowired
	@Transactional public void init(final SessionFactory sessionFactory) throws DataAccessException {		
		setSessionFactory(sessionFactory);
	}
	
	@Override
	public void save(final SerieDeleted serieDeleted) {
		getHibernateTemplate().save(serieDeleted);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SerieDeleted> getAll() {
		List<SerieDeleted> toReturn = getHibernateTemplate().find(
				" from SerieDeleted as sd");
		return toReturn;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SerieDeleted> getBySucursalRFC(final Sucursal sucursal, 
			final String RFC) {
		List<SerieDeleted> toReturn = new ArrayList<SerieDeleted>();
		if(sucursal == null) {
			toReturn = getHibernateTemplate().find(
					" from SerieDeleted as sd" +
					" where sd.RFC=?", RFC);
		}
		else {
			toReturn = getHibernateTemplate().find(
					" from SerieDeleted as sd" +
					" where sd.sucursal=? and" +
					" sd.RFC=?",sucursal, RFC);
		}
		return toReturn;
	}

}
